package com.jzit168.library_base.ext

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.permissionx.guolindev.PermissionX
import com.permissionx.guolindev.callback.RequestCallback

/**
 *@Desc: Fragment的扩展函数
 *@Author: Android Developer
 *@Date: 2020/10/15
 *
 **/

//跳转函数
fun <T> Fragment.start(clazz: Class<T>) {
    Intent(context, clazz).also {
        startActivity(it)
    }
}

//携带bundle的跳转函数
fun <T> Fragment.start(clazz: Class<T>, bundle: Bundle) {
    Intent(context, clazz).also {
        startActivity(it, bundle)
    }
}

//toast函数
fun Fragment.toast(msg: Any?) {
    Toast.makeText(context, "$msg", Toast.LENGTH_SHORT).show()
}

fun Fragment.requestPermission(
    permissions: MutableList<String>,
    whyRequest: String,
    deniedWhy: String,
    request: RequestCallback
) {
    PermissionX.init(this)
        .permissions(permissions)
        .onExplainRequestReason { scope, deniedList ->
            scope.showRequestReasonDialog(deniedList, whyRequest, "知道了")
        }
        .onForwardToSettings { scope, deniedList ->
            scope.showForwardToSettingsDialog(deniedList, deniedWhy, "知道了")
        }
        .request { allGranted, grantedList, deniedList ->
            request.onResult(allGranted, grantedList, deniedList)
        }
}